import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/response/service_order_list_data.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/ui/service_order/service_order_detail_page.dart';
import 'package:ls_work/viewmodel/service_order/service_order_list_view_model.dart';

class ServiceOrderListPage extends BaseStatefulMvvmWidget {
  final String statusId;

  ServiceOrderListPage(this.statusId);

  @override
  State<StatefulWidget> createState() => _ServiceOrderListState();
}

class _ServiceOrderListState
    extends BaseMvvmListState<ServiceOrderListViewModel, ServiceOrderListPage> {
  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return loadingContentWidget;
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Stack(
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              children: [
                CommonText('工单编号：${item.orderServeCode}',
                        textSize: 14, textColor: Color(0xff848689))
                    .setWeight(1),
                CommonText('${item.statusName}',
                    textSize: 15, textColor: statusColor(item.statusId!))
              ],
            ),
            Container(
              height: 5,
            ),
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Icon(
                  Icons.house_rounded,
                  color: Colors.white,
                )
                    .addToContainer(
                        decoration: BoxDecoration(
                            color: Color(0xff3EB9AC),
                            borderRadius: 8.borderRadius),
                        width: 40,
                        height: 40)
                    .addToContainer(margin: EdgeInsets.only(top: 4, right: 12)),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Container(
                      height: 8,
                    ),
                    Row(children: [
                      CommonText('${item.orderServeItemName}',
                          textSize: 18, fontWeight: FontWeight.w600)
                          .setWeight(1),
                      if (item.type != null)
                        CommonText(item.typeName,
                            textSize: 14, textColor: Colors.white)
                            .addToContainer(
                            decoration: BoxDecoration(
                              color: item.type == 1
                                  ? Colors.orange
                                  : Color(0xff3EB9AC),
                            ),
                            padding: EdgeInsets.symmetric(
                                horizontal: 8, vertical: 3))
                    ]),
                    Container(
                      height: 8,
                    ),
                    CommonText('预约时间：${item.markDuration ?? ""}',
                        textSize: 14, textColor: Color(0xff848689)),
                    Container(
                      height: 5,
                    ),
                    CommonText('服务对象：${item.orderServeHuman ?? ""}',
                        textSize: 14, textColor: Color(0xff848689)),
                    Container(
                      height: 5,
                    ),
                    CommonText('服务地址：${item.orderServeAddress ?? ""}',
                        textSize: 14, textColor: Color(0xff848689)),
                    Container(
                      height: 5,
                    ),
                    CommonText('联系电话：${item.humanMobile ?? ""}',
                        textSize: 14, textColor: Color(0xff848689)),
                    Container(
                      height: 5,
                    ),
                    CommonText('楼栋楼层：${item.buildingName ?? ""}-${item.floorName??""}',
                        textSize: 14, textColor: Color(0xff848689)),
                    if(!item.site.isNullOrEmpty())
                      ...[
                        Container(
                          height: 5,
                        ),

                        CommonText(
                            '站点：${item.site}',
                            textSize: 14,
                            textColor: Color(0xff848689)),
                      ],
                  ],
                ).setWeight(1)
              ],
            )
          ],
        ),
        statusHandle(item).isNotEmpty? ElevatedButton(
                onPressed: () {
                  if(item.orderServeTypeName?.contains("用餐")==true&&(item.statusId=="10235"||item.statusId=="10236")){
                    Go().push(ServiceOrderDetailPage(
                      showDetail: false,
                      orderId: item.id!,
                      handleStatus: "待完成",
                    ));
                  }else{
                    Go().push(ServiceOrderDetailPage(
                      showDetail: false,
                      orderId: item.id!,
                      // handleStatus: "待完成",
                    ));
                  }

                },
                style: ButtonStyle(
                    shape: WidgetStateProperty.all(StadiumBorder()),
                    elevation: WidgetStateProperty.all(0),
                    padding: WidgetStateProperty.all(EdgeInsets.symmetric(horizontal: 8)),
                    maximumSize:
                        WidgetStateProperty.all(Size.fromHeight(36))),
                child: CommonText(statusHandle(item), textColor: Colors.white, textSize: 14))
            .addToContainer(height: 30, alignment: Alignment.center)
            .setLocation(right: 0, bottom: 0):Container()
      ],
    ).onTap(() {
      Go().push(ServiceOrderDetailPage(
        showDetail: true,
        orderId: item.id!,
      ));
    }).addToContainer(
        decoration:
            BoxDecoration(color: Colors.white, borderRadius: 8.borderRadius),
        margin: EdgeInsets.symmetric(
          horizontal: 16,
        ),
        padding: EdgeInsets.all(10));
  }

  @override
  ServiceOrderListViewModel createViewModel() {
    return ServiceOrderListViewModel(context, widget.statusId);
  }

  @override
  EdgeInsets get listPadding => EdgeInsets.symmetric(vertical: 16);

  @override
  Widget get separatorDivider => Container(
        height: 10,
      );
}

Color statusColor(String statusId) {
  switch (statusId) {
    case '10233':
      return Color(0xffFD625F);
    case '10235':
      return Color(0xffFFB86F);
    case '10236':
      return Color(0xff3EB9AC);
    default:
      return Colors.blue;
  }
}

String statusHandle(ServiceOrderListData item){
  var statusId = item.statusId!;
  switch (statusId) {
    case '10233':
      return '接单';
    case '10235':
      if(item.orderServeTypeName?.contains("用餐")==true){
        return '结束服务';
      }
      return '开始服务';
    case '10236':
      return '结束服务';
    default:
      return '';
  }
}
